﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace AutoParkingManagement
{
    public partial class Login : Form
    {
        #region Private members
        private bool IsValidUser()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT e.PermissionID ");
            sb.Append("FROM Employee e LEFT JOIN PERMISSION p ON e.PermissionID=p.PermissionID ");
            sb.Append("WHERE e.Active =1 AND UserName=@UserName AND [Password]=@Password");
            string strCommandText = sb.ToString();
            SqlParameter[] parms = new SqlParameter[2];
            parms[0] = new SqlParameter();
            parms[0].ParameterName = "@UserName";
            parms[0].SqlDbType = SqlDbType.NVarChar;
            parms[0].Size = 50;
            parms[0].Value = Username;

            parms[1] = new SqlParameter();
            parms[1].ParameterName = "@Password";
            parms[1].SqlDbType = SqlDbType.NVarChar;
            parms[1].Size = 255;
            parms[1].Value = Password;

            object objPermission = SqlHelper.ExecuteScalar(CommandType.Text, strCommandText, parms);

            if (objPermission == null) return false;

            if (objPermission.ToString().Contains("01"))
            {
                Permission = Common.SYSTEM_PERMISSIONS.VEHICLE_KEEPER;
            }
            else if (objPermission.ToString().Contains("02"))
            {
                Permission = Common.SYSTEM_PERMISSIONS.ACCOUNTANT;
            }
            else if (objPermission.ToString().Contains("03"))
            {
                Permission = Common.SYSTEM_PERMISSIONS.MANAGER;
            }
            else if (objPermission.ToString().Contains("04"))
            {
                Permission = Common.SYSTEM_PERMISSIONS.ADMIN;
            }
            else if (objPermission.ToString().Contains("05"))
            {
                Permission = Common.SYSTEM_PERMISSIONS.SUPER_ADMIN;
            }

            return true;
        }
        #endregion

        #region Properties
        #endregion

        #region Public members
        public delegate void LoginChangedHandler(bool isLogin);
        public event LoginChangedHandler LoginChanged;
        public static string Username;
        public static string Password;
        public static Common.SYSTEM_PERMISSIONS Permission;
        #endregion

        #region Events
        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                Username = txtUserName.Text.Trim();
                Password = Common.Md5Encrypt(txtPassword.Text.Trim());
                Permission = Common.SYSTEM_PERMISSIONS.NONE;
                if (IsValidUser())
                {
                    ///////////////////////////////////////////////////////
                    this.Hide();
                    LoginChanged(true);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Tên đăng nhập \nhoặc mật khẩu không đúng", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi hệ thống.\n Vui lòng liên hệ quản trị hệ thống để được hỗ trợ.");
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled = (!String.IsNullOrEmpty(txtUserName.Text.Trim()));
        }
        #endregion

        #region Constructors
        public Login()
        {
            InitializeComponent();
        }
        #endregion
        

        
    }
}
